﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class Institutions : DetailView<institution>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<institution> EntityList;
        private new institution Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<Institutions>

        public override List<institution> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.institutions.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<institution> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.institutions.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();                         

            return EntityList;
        }

        public override institution GetEntity()
        {
            _context = new project5188Entities();
            Entity = new institution();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddToinstitutions(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<institution> GetEntityListByParameters(string institutionNo, string institutionName, DateTime? agreementStartDate, DateTime? agreementEndDate)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (!string.IsNullOrEmpty(institutionNo) && !string.IsNullOrWhiteSpace(institutionNo))
            {
                dynamicString += " And it.b_InstitutionNo = @InstitutionNo";
                parameterList.Add(new ObjectParameter("InstitutionNo", Convert.ToInt32(institutionNo)));
            }

            if (!string.IsNullOrEmpty(institutionName) && !string.IsNullOrWhiteSpace(institutionName))
            {
                dynamicString += " And it.c_InstitutionName = @InstitutionName";
                parameterList.Add(new ObjectParameter("InstitutionName", institutionName));
            }

            if (agreementStartDate != null)
            {
                dynamicString += " And it.v_AgreementStartDate = @AgreementStartDate";
                parameterList.Add(new ObjectParameter("AgreementStartDate", agreementStartDate));
            }

            if (agreementEndDate != null)
            {
                dynamicString += " And it.za_AgreementEndDate = @AgreementEndDate";
                parameterList.Add(new ObjectParameter("AgreementEndDate", agreementEndDate));
            }

            EntityList = _context.institutions.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
